package com.niit.onlinemusicserver34.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niit.onlinemusicserver34.common.R;
import com.niit.onlinemusicserver34.mapper.CommentMapper;
import com.niit.onlinemusicserver34.model.domain.Comment;
import com.niit.onlinemusicserver34.model.domain.Song;
import com.niit.onlinemusicserver34.model.request.CommentRequest;
import com.niit.onlinemusicserver34.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    /** 省略其他代码...... */

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public R addComment(CommentRequest addCommentRequest) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(addCommentRequest, comment);
        comment.setType(addCommentRequest.getNowType());
        comment.setCreateTime(new Date());
        if (commentMapper.insert(comment) > 0) {
            return R.success("评论成功");
        } else {
            return R.error("评论失败");
        }
    }

    @Override
    public R updateCommentMsg(CommentRequest addCommentRequest) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(addCommentRequest, comment);
        if (commentMapper.updateById(comment) > 0) {
            return R.success("点赞成功");
        } else {
            return R.error("点赞失败");
        }
    }

    //    删除评论
    @Override
    public R deleteComment(Integer id) {
        if (commentMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }
    /**
     * @Description: 根据歌单id获取评论列表
     * @param: songListId 歌单id
     * @return: com.example.online.music.common.R
     **/
    @Override
    public R getCommentListBySongListId(Integer songListId) {
        return R.success("查询成功", commentMapper.selectCommentListBySongListId(songListId));
    }

    @Override
    public R getCommentListBySongId(Integer songId) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("song_id",songId);
        return R.success("查询成功", commentMapper.selectList(queryWrapper));
    }

    /** 省略其他代码...... */
}